export default function (req, res, next) {
  const start = Date.now();

  res.on('finish', () => {
    const duration = Date.now() - start;
    // 获取客户端IP（考虑代理情况）
    const clientIp =
      req.headers['x-forwarded-for'] ||
      req.headers['x-real-ip'] ||
      req.connection.remoteAddress ||
      req.socket.remoteAddress ||
      req.ip;

    console.log(
      `[performance] [page_load_time] ${req.method} ${req.url} ${res.statusCode}- ${duration}ms -IP:${clientIp}`
    );
  });

  // res.on('close', () => {
  //   console.log(
  //     `[performance] [request_close] ${req.method} ${req.url} `
  //   );
  // });

  res.on('error', (err) => {
    console.log(`[performance] [request_err] ${req.method} ${req.url}`, err);
  });

  next();
}
